<template>

  <div class="department-container">

    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        组织架构
        <el-row :gutter="20">
          <el-col :span="16">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="8">
            <el-row>
              <el-col :span="4">负责人</el-col>
              <el-col :span="8">
                <el-dropdown>
                  <span class="el-dropdown-link">
                    下拉菜单<i class="el-icon-arrow-down el-icon--right" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- 一级部门的pid固定是'' -->
                    <el-dropdown-item @click.native="hAdd('')">添加1级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 主题内容 -->
        <el-tree :data="list" :props="defaultProps" @node-click="handleNodeClick">
          <template slot-scope="{data}">
            <el-row :gutter="20" style="width:100%">
              <el-col :span="16">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="8">
                <el-row>
                  <el-col :span="4">{{ data.manager }}</el-col>
                  <el-col :span="8">
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        下拉菜单<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <!-- handler：处理 -->
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <!-- 添加/编辑 -->
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="isShowDialog"
      @close="hDialogClose"
    >
      <!--  -->
      <!-- v-if="isShowDialog" -->
      <!-- 放一个子组件 -->
      <deptDialog
        :id="curId"
        ref="refDept"
        :is-edit="isEdit"
        :origin-list="originList"
        @success="hSuccess"
        @close="hClose"
      />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import deptDialog from './deptDialog.vue'
import { tranListToTreeData } from '@/utils/index'
export default {
  components: { deptDialog },
  data() {
    return {
      originList: [], // 传给子组件做表单校验

      isEdit: false, // 当前是否是编辑状态
      curId: '', // 当前选择的部门的id
      isShowDialog: false, // 控制dialog
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hDialogClose() {
      // 重置子组件中的表单
      this.$refs.refDept.resetForm()
    },
    // 收到子组件内派发的success事件
    hSuccess() {
      console.log('success')
      this.isShowDialog = false
      this.loadDepartments()
    },
    // 收到子组件内派发的close事件
    hClose() {
      this.isShowDialog = false
    },
    // 用户点击了添加
    hAdd(id) {
      // console.log(id)
      // 弹框
      this.isShowDialog = true
      // 更新当前部门的id
      this.curId = id

      this.isEdit = false
    },
    hDel(id) {
      // alert(id)
      this.$confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    async doDel(id) {
      await delDepartment(id)
      this.$message.success('删除成功')
      this.loadDepartments()
    },
    hEdit(id) {
      // 弹框
      this.isShowDialog = true
      // 更新当前部门的id
      this.curId = id
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.refDept.loadDepartmentDetail()
      })
    },
    async loadDepartments() {
      // 调用接口，给list赋值
      const res = await getDepartments()
      // 第一项是公司的名字，不应该是某个部门，所以去掉它。
      res.data.depts.shift()

      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          code: item.code,
          name: item.name,
          pid: item.pid
        }
      })

      this.list = tranListToTreeData(res.data.depts)
    },
    handleNodeClick(data) {
      console.log(data)
    }
  }
}
</script>
